package com.center.growcenterserver.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.center.growcenterserver.common.R;
import com.center.growcenterserver.mapper.EventMapper;
import com.center.growcenterserver.model.domain.Event;
import com.center.growcenterserver.model.request.EventRequest;
import com.center.growcenterserver.service.EventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class EventServiceImpl extends ServiceImpl<EventMapper, Event> implements EventService {

    @Autowired
    private EventMapper eventMapper;

    @Override
    public R getAllEvents() {
        return R.success("查询成功", eventMapper.getAllEvents());
    }

    @Override
    public R getAllEvents(IPage<Event> page) {
        IPage<Event> events = this.page(page);
        if (events.getRecords().isEmpty()) {
            return R.error("未查询到任何事件");
        }
        return R.success("查询成功", events);
    }

    @Override
    public R getEventById(String id) {
        Event event = this.getById(id);
        if (event == null) {
            return R.error("未找到指定ID的事件");
        }
        return R.success("查询成功", event);
    }

    @Override
    public int addEvent(EventRequest eventRequest) {
        Event event = new Event();

        // 将请求对象转换为实体对象，但忽略 id 字段
        BeanUtils.copyProperties(eventRequest, event); // 忽略 id 字段的复制

        // 检查 location 字段是否为空
        if (event.getLocation() == null || event.getLocation().isEmpty()) {
            // 设置默认值，例如 "Unknown Location"
            event.setLocation("Unknown Location");
        }

        // 如果前端未传递 date，可以设置默认值
        if (event.getDate() == null) {
            event.setDate(new Date());
        }

        // 保存到数据库，id 会由数据库自动生成
        return eventMapper.insert(event);
    }

    @Override
    public R updateEvent(EventRequest eventRequest) {
        Event event = new Event();
        BeanUtils.copyProperties(eventRequest, event); // 将请求对象转换为实体对象
        boolean result = this.updateById(event); // 更新事件信息
        if (result) {
            return R.success("事件更新成功");
        } else {
            return R.error("事件更新失败");
        }
    }

    @Override
    @Transactional // 开启事务
    public R deleteEventById(Long id) {
        boolean result = this.removeById(id); // 根据ID删除事件
        if (result) {
            return R.success("事件删除成功");
        } else {
            return R.error("事件删除失败");
        }
    }

    @Override
    public R deleteEventsByIds(Long[] ids) {
        boolean result = this.removeByIds(List.of(ids)); // 批量删除事件
        if (result) {
            return R.success("事件批量删除成功");
        } else {
            return R.error("事件批量删除失败");
        }
    }
}
